package com.ossjk.oa.system.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.formula.Formula;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.core.util.Constant;
import com.ossjk.core.vo.ResponseBean;
import com.ossjk.core.vo.ZtreeBean;
import com.ossjk.oa.system.entity.Department;
import com.ossjk.oa.system.entity.Permission;
import com.ossjk.oa.system.entity.Post;
import com.ossjk.oa.system.service.IDepartmentService;
import com.ossjk.oa.system.service.IPermissionService;
import com.ossjk.oa.system.service.IPostPermissionService;
import com.ossjk.oa.system.service.IPostService;

/**
 * <p>
 * 岗位表 前端控制器
 * </p>
 *
 * @author chair
 * @since 2019-01-23
 */
@RestController
@RequestMapping("/system/post")
public class PostController extends BaseController {
	@Autowired
	private IPostService iPostService;
	@Autowired
	private IDepartmentService iDepartmentService;
	@Autowired
	private IPermissionService iPermissionService;
	@Autowired
	private IPostPermissionService iPostPermissionService;

	@RequiresPermissions("00000010")
	@GetMapping("/list")
	public Object list(String name, Page page) {
		// 无需分页
		page = new Page(1, Integer.MAX_VALUE);
		Wrapper wrapper = Condition.create().orderBy("level", true).orderBy("sort", true);
		page = iPostService.selectPage(page, wrapper);
		return responseSuccess(page);
	}

	@RequiresPermissions("000000100020")
	@GetMapping("/toInsert")
	public Object toInsert() {
		// 查找所有部门
		Wrapper wrapper = Condition.create().orderBy("level", true).orderBy("sort", true);
		List<Department> departments = iDepartmentService.selectList(wrapper);
		List<ZtreeBean> ztreeBeans = new ArrayList();
		// 转换ztreeBean
		if (!CommonUtil.isBlank(departments)) {
			for (Department department : departments) {
				ZtreeBean ztreeBean = new ZtreeBean();
				ztreeBean.setId(department.getId());
				ztreeBean.setPId(department.getPid());
				ztreeBean.setName(department.getName());
				ztreeBean.setOpen(true);
				ztreeBeans.add(ztreeBean);
			}
		}
		return responseSuccess(ztreeBeans);
	}

	@RequiresPermissions("000000100020")
	@PostMapping("/insert")
	public Object insert(Post post) {

		if (CommonUtil.isEquals(post.getLevel(), 1)) {
			post.setPid("0");
		}
		if (iPostService.insert(post)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	@RequiresPermissions("000000100005")
	@GetMapping("/toUpdate/{id}")
	public Object toUpdate(@PathVariable("id") String id) {
		// 查找所有部门
		Wrapper wrapper = Condition.create().orderBy("level", true).orderBy("sort", true);
		List<Department> departments = iDepartmentService.selectList(wrapper);
		List<ZtreeBean> ztreeBeans = new ArrayList();
		// 转换ztreeBean
		if (!CommonUtil.isBlank(departments)) {
			for (Department department : departments) {
				ZtreeBean ztreeBean = new ZtreeBean();
				ztreeBean.setId(department.getId());
				ztreeBean.setPId(department.getPid());
				ztreeBean.setName(department.getName());
				ztreeBean.setOpen(true);
				ztreeBeans.add(ztreeBean);
			}
		}
		Map map = new HashMap();
		Post post = iPostService.selectById(id);
		map.put("ztreeBeans", ztreeBeans);
		map.put("record", post);
		map.put("department", iDepartmentService.selectById(post.getDid()));
		return responseSuccess(map);
	}

	@RequiresPermissions("000000100005")
	@PutMapping("/update")
	public Object update(Post post) {
		if (iPostService.updateById(post)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	@RequiresPermissions("000000100015")
	@DeleteMapping("/delete/{id}")
	public Object delete(@PathVariable("id") String[] id) {
		if (iPostService.deleteBatchIds(Arrays.asList(id))) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	@RequiresPermissions("000000100010")
	@GetMapping("/toUpdatePermission/{id}")
	public Object toUpdatePermission(@PathVariable("id") String id) {
		// 查找该岗位的权限
		List<String> perids = iPostPermissionService.selectPeridByPid(id);
		Wrapper wrapper = Condition.create().orderBy("level", true).orderBy("sort", true);
		List<Permission> permissions = iPermissionService.selectList(wrapper);
		// 把查询回来的实体转成ZtreeBean
		List<ZtreeBean> ztreeBeans = new ArrayList();
		if (!CommonUtil.isBlank(permissions)) {
			for (Permission p : permissions) {
				ZtreeBean ztreeBean = new ZtreeBean();
				ztreeBean.setId(p.getId() + "");
				// ztreeBean.setIsParent(CommonUtil.isBlank(p.getPid()));
				ztreeBean.setName(p.getName());
				ztreeBean.setPId(p.getPid());
				ztreeBean.setOpen(true);
				// 匹配该权限是否已经选中
				if (!CommonUtil.isBlank(perids)) {
					ztreeBean.setChecked(perids.contains(p.getId()));
				}
				ztreeBeans.add(ztreeBean);
			}
		}
		return responseSuccess(ztreeBeans);
	}

	@RequiresPermissions("000000100010")
	@PutMapping("/updatePermission")
	public Object updatePermission(String pid, String[] perid) {
		iPostPermissionService.updatePermission(pid, perid);
		return responseSuccess();
	}

	@RequiresPermissions("000000100005")
	@GetMapping("/toExpression/{id}")
	public Object toExpression(@PathVariable("id") String id) {
		Post post = iPostService.selectById(id);
		// 表达式
	System.out.println();
		post.getFormula();
		return null;

	}
}
